package cn.bone.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {

    /**
     * @title	发送get请求
     * @param url
     * @param heads
     * @return
     * @author	陈义龙
     * @date	2016年11月21日 下午2:31:27
     */
    public static String get(String url,Map<String,String> heads){

        if(heads==null){
            heads = new HashMap<String,String>();
        }

        OkHttpClient client = new OkHttpClient();
        okhttp3.Request.Builder builder = new Request.Builder().url(url);
        for(Map.Entry<String, String> entry : heads.entrySet()){
            builder = builder.addHeader(entry.getKey(), entry.getValue());
        }

        String result = null;
        Request request = builder.get().build();
        try {
            Response response = client.newCall(request).execute();
            int code = response.code();
            result = response.body().string();
            System.out.println(code);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;

    }


    /**
     * @title	使用okhttp,post发送流数据
     * @param url	请求地址
     * @param heads	请求头
     * @param content	流数据文本
     * @return
     * @author	陈义龙
     * @date	2016年10月18日 上午9:15:30
     */
    public static String post(String url,Map<String,String> heads,String content){

        String result = null;
        byte[] bytes = null;
        try{
            bytes = content.getBytes("UTF-8");
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(content);
        MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType, bytes);

        OkHttpClient client = new OkHttpClient();
        okhttp3.Request.Builder builder = new Request.Builder().url(url);
        for(Map.Entry<String, String> entry : heads.entrySet()){
            builder = builder.addHeader(entry.getKey(), entry.getValue());
        }
        Request request = builder.post(requestBody).build();
        try {
            Response response = client.newCall(request).execute();
            int code = response.code();
            result = response.body().string();
            System.out.println(code);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @title	使用okhttp,post发送表单数据
     * @param url	请求地址
     * @param formBeans	请求表单
     * @return
     * @author	陈义龙
     * @date	2016年10月18日 上午9:49:39
     */
    public static String post(String url,List<FormBean> formBeans){

        return post(url,null,formBeans);
    }

    /**
     * @title	使用okhttp,post发送表单数据
     * @param url	请求地址
     * @param heads	请求头
     * @param formBeans	请求表单
     * @return
     * @author	陈义龙
     * @date	2016年10月18日 上午9:49:39
     */
    public static String post(String url,Map<String,String> heads,List<FormBean> formBeans){

        if(heads==null){
            heads = new HashMap<String,String>();
        }
        if(formBeans==null){
            formBeans = new ArrayList<HttpUtil.FormBean>();
        }

        boolean haveFile = false;
        for(FormBean bean : formBeans){
            if(bean.isFile){
                haveFile = true;
                break;
            }
        }
        RequestBody requestBody;
        if(haveFile){
            MediaType mediaType = MediaType.parse("image/png");
            MultipartBody.Builder builder = new MultipartBody.Builder();
            for(FormBean bean : formBeans){
                if(bean.isFile){
                    builder = builder.addFormDataPart(bean.getKey(),
                            bean.getFile().getName(),
                            RequestBody.create(mediaType, bean.getFile()));
                }else{
                    builder = builder.addFormDataPart(bean.getKey(), bean.getValue());
                }
            }
            requestBody = builder.build();
        }else{
            FormBody.Builder builder = new FormBody.Builder();
            for(FormBean bean : formBeans){
                builder = builder.add(bean.getKey(), bean.getValue());
            }
            requestBody = builder.build();
        }
        Request.Builder requestBuilder = new Request.Builder();
        for(Map.Entry<String, String> entry : heads.entrySet()){
            requestBuilder.addHeader(entry.getKey(), entry.getValue());
        }
        Request request = requestBuilder.url(url).post(requestBody).build();
        String result = null;
        //OkHttpClient client = new OkHttpClient();
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .connectTimeout(100, TimeUnit.SECONDS)
                .build();
        try {
            Response response = client.newCall(request).execute();
            int code = response.code();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static class FormBean {
        private String key;
        private String value;
        private boolean isFile = false;
        private File file;

        // 文本参数的构造方法
        public FormBean(String key, String value) {
            this.key = key;
            this.value = value;
            this.isFile = false;
            this.file = null;
        }

        // 文件参数的构造方法
        public FormBean(String key, File file) {
            this.key = key;
            this.isFile = true;
            this.file = file;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public boolean isFile() {
            return isFile;
        }

        public void setFile(boolean isFile) {
            this.isFile = isFile;
        }

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }
    }
}

